<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * 发送邮件
 * @param  [type] $address  发送地址
 * @param  [type] $body     邮件内容
 * @param  [type] $Subject  邮件标题
 * @param  [type] $fromname 发送人姓名
 * @return [type]           [description]
 */
function sendEmail($address, $body, $Subject, $fromname)
{

    $mail = new \PHPMailer\PHPMailer\PHPMailer(); //实例化
    $mail->IsSMTP(); //启用SMTP

    $mail->Host = config('webconfig.mailhost'); //SMTP服务器 以126邮箱为例子
    $mail->Port = config('webconfig.mailport'); //邮件发送端口

    $mail->SMTPAuth = true; //启用SMTP认证

    $mail->SMTPSecure = "ssl"; //设置安全验证方式为ssl
    $mail->CharSet    = "UTF-8"; //字符集
    $mail->Encoding   = "base64"; //编码方式

    $mail->Username = config('webconfig.mail'); //你的邮箱
    $mail->Password = config('webconfig.mailpwd'); //你的密码
    $mail->Subject  = $Subject; //邮件标题
    $mail->From     = config('webconfig.mail'); //发件人地址（也就是你的邮箱）
    //if(isset($fromname)
    $mail->FromName = isset($fromname) ? $fromname : '网站来信'; //发件人姓名

    //
    $mail->AddAddress($address, "亲"); //添加收件人（地址，昵称）
    $mail->IsHTML(true);
    $mail->Body = $body;

    return ($mail->Send());

}


function object_to_array($obj) {
    $obj = (array)$obj;
    foreach ($obj as $k => $v) {
        if (gettype($v) == 'resource') {
            return;
        }
        if (gettype($v) == 'object' || gettype($v) == 'array') {
            $obj[$k] = (array)object_to_array($v);
        }
    }
 
    return $obj;
}
